//
//  HOTEvaluationListViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-26.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTEvaluationListViewController.h"
#import "HOTEvaluationListCell.h"

@interface HOTEvaluationListViewController () <UITableViewDelegate, UITableViewDataSource> {
    
    NSInteger _pageNo;
}
@property(nonatomic,copy)NSString *userId;
@property(nonatomic,strong)NSMutableArray *dataList;
@end

@implementation HOTEvaluationListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil userId:(NSString *)userId_
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.userId = userId_;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNaviTitle:@"評価"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    _tableView.showFooter = YES;
    _tableView.showHeader = YES;
    
    __unsafe_unretained HOTEvaluationListViewController *tempObj = self;
    _tableView.headerView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        _pageNo = 1;
        [tempObj requestDataFromNet];
    };
    
    _tableView.footerView.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        _pageNo ++;
        [tempObj requestDataFromNet];
    };
    
    self.dataList = [[NSMutableArray alloc] init];
    
    _pageNo = 1;
    [self requestDataFromNet];
}

-(void)requestDataFromNet {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_TARGET_UID:[HOTUtils getString:self.userId],
                                 REQ_KEY_P_NUM:@(_pageNo),
                                 REQ_KEY_P_SIZE:kPageCnt};
    
    __unsafe_unretained HOTEvaluationListViewController *tempObj = self;
    [self requestWithURL:HOT_URL_GOAL_LIST Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        [tempObj loadingSuccess];
        [_tableView.headerView endRefreshing];
        [_tableView.footerView endRefreshing];
        if (dataArr.count == 0) {
            
            if (_pageNo == 1) {
                
                [tempObj.dataList removeAllObjects];
                [_tableView reloadData];
                [self alertNoDataMessage:^{
                    //[tempObj.navigationController popViewControllerAnimated:YES];
                }];
            } else {
                [self alertNoDataAgainMessage:nil];
            }
            
            
            return;
        }
        
        if (_pageNo == 1) {
            [tempObj.dataList removeAllObjects];
        }
        [tempObj.dataList addObjectsFromArray:dataArr];
        
        [_tableView reloadData];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        [_tableView.headerView endRefreshing];
        [_tableView.footerView endRefreshing];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTEvaluationListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTEvaluationListCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    cell.noLabel.text = [NSString stringWithFormat:@"%d.",(indexPath.row +1)];
    cell.dateTimeLabel.text = [dic objectForKey:@"rate_time"];
    cell.titleLabel.text = [dic objectForKey:@"rate_title"];
    cell.contentLabel.text = [dic objectForKey:@"rate_content"];
    [cell.ratingView displayRating :[[dic objectForKey:@"score"] doubleValue]];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTEvaluationListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTEvaluationListCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    cell.noLabel.text = [NSString stringWithFormat:@"%d.",(indexPath.row +1)];
    cell.dateTimeLabel.text = [dic objectForKey:@"rate_time"];
    cell.titleLabel.text = [dic objectForKey:@"rate_title"];
    cell.contentLabel.text = [dic objectForKey:@"rate_content"];
    [cell.ratingView displayRating :[[dic objectForKey:@"score"] doubleValue]];
    
    [cell setNeedsDisplay];
    [cell setNeedsLayout];
    [cell.contentView setNeedsLayout];
    [cell.contentView setNeedsDisplay];
    
    float cellH = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return cellH +1;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
